package com.easondongh.system.controller;

import com.easondongh.system.domain.SysDept;
import com.easondongh.common.enums.ResponseEnum;
import com.easondongh.common.response.ResponseObj;
import com.easondongh.system.service.SysDeptService;
import com.easondongh.system.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author EasonDongH
 * @date 2020/5/6 15:47
 */
@Slf4j
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private SysDeptService deptService;

    /**
     * 加载所有部门，按order_num正序排序
     * @return
     */
    @GetMapping("/loadDept")
    public ResponseObj<List<SysDept>> loadDept() {
        List<SysDept> list = this.deptService.loadDept();
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, list);
    }

    /**
     * 排除id = #{id} 及 其所有子节点、孙子节点等
     * @param id
     * @return
     */
    @GetMapping("/getDeptExcludeById")
    public ResponseObj<List<SysDept>> getDeptExcludeById(Integer id) {
        if(id == null) {
            return ResponseObj.QueryFail;
        }
        List<SysDept> list = this.deptService.getDeptExcludeById(id);
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, list);
    }

    /**
     * 获取当前部门的最大排序号
     * @return
     */
    @GetMapping("/getMaxOrderNum")
    public ResponseObj<Integer> getMaxOrderNum() {
        Integer orderNum = this.deptService.getMaxOrderNum();
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, orderNum);
    }

    /**
     * 新增部门
     * @param dept
     * @return
     */
    @PostMapping("/addDept")
    @RequiresPermissions({"dept:add"})
    public ResponseObj addDept(@RequestBody SysDept dept) {
        if(dept == null || StringUtils.isBlank(dept.getTitle())) {
            log.warn("添加部门时部门标题不能为空");
            return new ResponseObj(ResponseEnum.ADD_FAIL);
        }
        if(dept.getOrderNum() == null) {
            dept.setOrderNum(this.deptService.getMaxOrderNum() + 1);
        }
        if(dept.getCreateTime() == null) {
            dept.setCreateTime(new Date());
        }
        this.deptService.save(dept);
        return new ResponseObj(ResponseEnum.ADD_SUCCESS);
    }

    /**
     * 根据id查询部门
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public ResponseObj<SysDept> getById(Integer id) {
        if(id == null) {
            return ResponseObj.QueryFail;
        }
        SysDept dept = this.deptService.getById(id);
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, dept);
    }

    /**
     * 统计该id下的子部门数量
     * @param id
     * @return
     */
    @GetMapping("/countChildDeptById")
    public ResponseObj<Integer> countChildDeptById(Integer id) {
        if(id == null) {
            log.warn("统计子部门数量时id不能为空");
            return ResponseObj.QueryFail;
        }
        Integer count = this.deptService.countChildDeptById(id);
        return new ResponseObj<Integer>(ResponseEnum.QUERY_SUCCESS, count);
    }

    /**
     * 根据id删除部门
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById")
    public ResponseObj deleteById(Integer id) {
        this.deptService.removeById(id);
        return ResponseObj.DeleteSuccess;
    }

    /**
     * 根据id更新部门
     * @param dept
     * @return
     */
    @PutMapping("/updateById")
    public ResponseObj updateById(@RequestBody SysDept dept) {
        if(dept == null || dept.getId() == null) {
            log.warn("updateById修改部门信息时部门id不能为空");
            return ResponseObj.UpdateFail;
        }
        this.deptService.updateById(dept);
        return ResponseObj.UpdateSuccess;
    }
}
